using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using GonzalesCore.CoreElements;
using GonzalesCore.CoreManagers;

using Jamila2.CoreElements;
using Jamila2.Database;

namespace BPCSFileDaemon.FileFactory
{
    public class ProjectFileFactory : FileFactoryBase
    {
        SystemUsers UsersCollection = null;
        protected readonly int LineLength = 63;

        public ProjectFileFactory(string path, string DBConnectionString, TypeOfDatabase DatabaseType)
            : base(path, DBConnectionString, DatabaseType)
        {
        }

        public override bool ReadFile()
        {
            bool smooth = true;

            if ((smooth = base.ReadFile()))
            {
                if (this.FileLines != null)
                {
                    this.UsersCollection = new SystemUsers();

                    using (RawProjectManager CurDataManager = new RawProjectManager(this.CurrentSystemSession))
                    {
                        // execute pre-action before inject data
                        CurDataManager.ExecutePreInjectAction();

                        // start to analyze
                        for (int x = 0; x < this.FileLines.Count; x++)
                        {
                            try
                            {
                                string CurrentLine = this.FileLines[x].ToString();

                                // break by offset
                                if (CurrentLine.Length >= this.LineLength)  // 174 is the only length
                                {
                                    string EntityID = CurrentLine.Substring(0, 2).Trim();    // size: 2
                                    string ProjectID = CurrentLine.Substring(2, 10).Trim();    // size: 10
                                    string ProjectDescription = CurrentLine.Substring(12, 50).Trim();    // size: 50
                                    string ProjectType = CurrentLine.Substring(62, 1).Trim();      // size: 1

                                    CurDataManager.AddData(EntityID, ProjectID, ProjectDescription, ProjectType);
                                    /*
                                    Console.WriteLine("EntityID: " + EntityID);
                                    Console.WriteLine("ProjectID: " + ProjectID);
                                    Console.WriteLine("ProjectDescription: " + ProjectDescription);
                                    Console.WriteLine("ProjectType: " + ProjectType);
                                    Console.WriteLine();
                                    */
                                }
                            }
                            catch (Exception ex)
                            {
                                smooth = false;
                                this.ErrMsg = "[ExRateFileFactory.ReadFile] : Exception : " + ex.Message;
                            }
                        }   // for

                        // inject now
                        
                        if (CurDataManager.TotalInTheList > 0)
                        {
                            CurDataManager.ExecuteFinalAction();

                            if (!CurDataManager.ExecuteDataInject())
                            {
                                smooth = false;
                                this.ErrMsg = "[ProjectFileFactory.ReadFile] : CurDataManager.ExecuteDataInject() : " + CurDataManager.ErrorMessage;                               

                            }
                        }
                        
                    }
                }
            }

            return smooth;
        }

    }
}
